<?php

namespace App\Service;

use AlibabaCloud\SDK\Green\V20220302\Models\TextModerationPlusRequest;
use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use AlibabaCloud\SDK\Green\V20220302\Green;

class GreenCode
{
    public function getWordCodeInfo($content)
    {
        $config = new Config([/**
         * 阿里云账号AccessKey拥有所有API的访问权限，建议您使用RAM用户进行API访问或日常运维。
         * 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里，否则可能导致AccessKey泄露，威胁您账号下所有资源的安全。
         * 常见获取环境变量方式：
         * 获取RAM用户AccessKey ID：getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         * 获取RAM用户AccessKey Secret：getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
            "accessKeyId" => config("omnipay.RAM.AccessKeyID"),// '建议从环境变量中获取RAM用户AccessKey ID',
            "accessKeySecret" => config("omnipay.RAM.AccessKeySecret"), //'建议从环境变量中获取RAM用户AccessKey Secret',
            // 设置HTTP代理。
            // "httpProxy" => "http://10.10.xx.xx:xxxx",
            // 设置HTTPS代理。
            // "httpsProxy" => "https://10.10.xx.xx:xxxx",
            "endpoint" => "green-cip.cn-shanghai.aliyuncs.com",
            "regionId" => "cn-shanghai"]);
// 注意，此处实例化的client请尽可能重复使用，避免重复建立连接，提升检测性能。
        $client = new Green($config);

        $request = new TextModerationPlusRequest();
        $request->service = "llm_query_moderation";
        $serviceParameters = array("content" => $content);
        $request->serviceParameters = json_encode($serviceParameters);
        $runtime = new RuntimeOptions();
        $runtime->readTimeout = 6000;
        $runtime->connectTimeout = 3000;
        try {
            $response = $client->textModerationPlusWithOptions($request, $runtime);
            if (200 != $response->statusCode) {
                return $response->statusCode;
                print_r("response not success. code:" . $response->statusCode);
                return;
            }
            $body = $response->body;
//            print_r("requestId = " . $body->requestId . "\n");
//            print_r("code = " . $body->code . "\n");
//            print_r("message = " . $body->message . "\n");
            if (200 != $body->code) {
                return false;
                print_r("text moderation not success. code:" . $body->code);
            }
            $data = $body->data;
            $result = (json_decode(json_encode($data), true));
            if($result['riskLevel'] == 'none'){
                return true;
            }
            if (isset($result['result']) && !empty($result['result'])) {
                //命中自定义库
                if(isset($result['result'][0]['customizedHit']) && !empty($result['result'][0]['customizedHit'])){
                    return $result['result'][0]['customizedHit'][0]['keyWords'];
                }
                //命中系统策略：
                if(isset($result['advice']) && !empty($result['advice'])){
                    return $result['result'][0]['riskWords'];
                }
            }
//            print_r("data = " . json_encode($data));
        } catch
        (TeaUnableRetryError $e) {
            var_dump($e->getMessage());
            var_dump($e->getErrorInfo());
            var_dump($e->getLastException());
            var_dump($e->getLastRequest());
        }
    }


}





